package cn.cocho.android.mc.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import cn.cocho.android.mc.log.LoggerUtils;
import cn.cocho.android.mc.util.CommonContexts;

/**
 * 根据当前的公司配置，自动跳转至当前公司
 * 
 * @author HAIKANG SONG
 * @time 2013-4-24下午2:34:54
 */

public class ActivityCompanyManager {

	/**
	 * 获得当前公司的Intent对象
	 * 
	 * @param context
	 * @param type
	 * @return
	 * @author HAIKANG SONG
	 */
	public static Intent getIntent(Context context, Class<?> type) {
		Intent intent = null;
		Class<?> target = null;
		String classPath = "";
		try {
			classPath = getCompanyClassPath(type);
			if (!classPath.equals(type.getName())) {// 如果引用的类，和当前公司的类不是同一个则需要重新初始化
				target = Class.forName(classPath);
			} else {
				target = type;
			}
		} catch (ClassNotFoundException e) {
			log(classPath, type.getName());
			target = type;
		}
		if (context != null && target != null) {
			intent = new Intent(context, target);
		}
		return intent;
	}

	/**
	 * 获得当前公司的component对象
	 * 
	 * @param context
	 * @param type
	 * @return
	 * @author HAIKANG SONG
	 */
	public static ComponentName getComponent(Context context, Class<?> type) {
		ComponentName component = null;
		Class<?> target = null;
		String classPath = "";
		try {
			classPath = getCompanyClassPath(type);
			if (!classPath.equals(type.getName())) {// 如果引用的类，和当前公司的类不是同一个则需要重新初始化
				target = Class.forName(classPath);
			} else {
				target = type;
			}
		} catch (ClassNotFoundException e) {
			log(classPath, type.getName());
			target = type;
		}
		if (context != null && target != null) {
			component = new ComponentName(context, target);
		}
		return component;
	}

	/**
	 * 获得当前公司的实体对象
	 * 
	 * @param obj
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getObject(Class<T> type) throws IllegalAccessException, InstantiationException {
		T obj = null;
		Class<?> target = null;
		String classPath = "";
		try {
			classPath = getCompanyClassPath(type);
			target = Class.forName(classPath);
		} catch (ClassNotFoundException e) {
			log(classPath, type.getName());
			obj = (T) type.newInstance();
		}
		obj = (T) target.newInstance();
		return obj;
	}

	/**
	 * 获得当前公司的类路径
	 * 
	 * @param type
	 *            公司对应的包名
	 * @return
	 * @author HAIKANG SONG
	 */
	private static String getCompanyClassPath(Class<?> type) {
		String classPath = type.getName();
		String newPath = "";
		String[] names = classPath.split("\\.");
		for (int i = 0; i < names.length; i++) {
			if (i == 3) {// 更换公司包名
				newPath += CommonContexts.companyName + ".";
			} else if (i == names.length - 1) {// 最后一个不需要添加“.”
				newPath += names[i];
			} else {
				newPath += names[i] + ".";
			}
		}
		return newPath;
	}

	/**
	 * 记录日志
	 * 
	 * @param newClassPath
	 * @param oldClassPath
	 */
	private static void log(String newClassPath, String oldClassPath) {
		LoggerUtils.warn(ActivityCompanyManager.class.getName(), "[" + CommonContexts.companyName + "]公司下不存在[" + newClassPath + "]，将跳转到[" + oldClassPath
				+ "]");
	}

}
